import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import AsyncStorage from '@react-native-async-storage/async-storage';

interface AuthState {
  accessToken: string | null;
  refreshToken: string | null;
  userInfo: any | null; // 应该定义更具体的类型
  setAccessToken: (token: string | null) => void;
  setRefreshToken: (token: string | null) => void;
  setUserInfo: (info: any | null) => void;
  clearAuth: () => void;
}

export const useAuthStore = create<AuthState>()(
  persist(
    (set) => ({
      accessToken: null,
      refreshToken: null,
      userInfo: null,
      setAccessToken: (token) => set({ accessToken: token }),
      setRefreshToken: (token) => set({ refreshToken: token }),
      setUserInfo: (info) => set({ userInfo: info }),
      clearAuth: () => set({ accessToken: null, refreshToken: null, userInfo: null }),
    }),
    {
      name: 'auth-storage', // 在 AsyncStorage 中的 key
      storage: createJSONStorage(() => AsyncStorage),
    }
  )
);
